import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Enumeration;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

@SuppressWarnings("serial")
public class ViewTitleDeedsGUI extends JPanel implements ActionListener {
	
	static String lots = "Lots";
	static String trains = "Trains";
	static String utilities = "Utilities";
	static String[] lotsList = new String [22];
	static String[] trainsList = new String [4];
	static String[] utilitiesList = new String [2];
	
	static JFrame frame;
	static JLayeredPane layeredPane;
	static JFrame labelFrame;
	static JLabel deedLabel = new JLabel(createImageIcon(LotTiles.values()[0].getTileURL()));
	static JLabel ownerLabel;
	static JRadioButton lotRadioButton;
	static JRadioButton trainRadioButton;
	static JRadioButton utilityRadioButton;
	static JComboBox lotComboBox;
	static JComboBox trainComboBox;
	static JComboBox utilityComboBox;
	static ButtonGroup group;
	static JPanel radioPanel;
	static JPanel deedPanel;
	static JPanel buttonPanel;
	static JButton viewButton;
	static JButton closeButton;
	
	public ViewTitleDeedsGUI() {
		
		frame = new JFrame("View Title Deeds");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setAlwaysOnTop(true);
		
		layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(479, 457));
		
		String owner;
		
		if (LotTiles.values()[0].getOwner() == null) {
			
			owner = " - ";
		}
		
		else {
			
			owner = LotTiles.values()[0].getOwner().getName();
		}
		
		ownerLabel = new JLabel(owner);
		
		lotRadioButton = new JRadioButton(lots);
        lotRadioButton.setMnemonic(KeyEvent.VK_B);
        lotRadioButton.setActionCommand(lots);
        lotRadioButton.setSelected(true);
		
		trainRadioButton = new JRadioButton(trains);
        trainRadioButton.setMnemonic(KeyEvent.VK_B);
        trainRadioButton.setActionCommand(trains);
		
        utilityRadioButton = new JRadioButton(utilities);
        utilityRadioButton.setMnemonic(KeyEvent.VK_B);
        utilityRadioButton.setActionCommand(utilities);
        
        this.populateLists();
        
        lotComboBox = new JComboBox(lotsList);
		lotComboBox.setSelectedIndex(0);
		lotComboBox.addActionListener(this);
		
		trainComboBox = new JComboBox(trainsList);
		trainComboBox.setSelectedIndex(0);
		trainComboBox.addActionListener(this);
		trainComboBox.setEnabled(false);
		
		utilityComboBox = new JComboBox(utilitiesList);
		utilityComboBox.setSelectedIndex(0);
		utilityComboBox.addActionListener(this);
		utilityComboBox.setEnabled(false);
        
        //creating the buttons
		closeButton = new JButton("Close");
		//closeButton.setPreferredSize(new Dimension(65, 32));
		closeButton.setMnemonic(KeyEvent.VK_M);
		closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });
		
		viewButton = new JButton("View");
		//viewButton.setPreferredSize(new Dimension(65, 32));
		viewButton.setMnemonic(KeyEvent.VK_M);
		viewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewButtonActionPerformed(evt);
            }
        });
		
		//creating the button group
		group = new ButtonGroup();
		group.add(lotRadioButton);
		group.add(trainRadioButton);
		group.add(utilityRadioButton);
		
		lotRadioButton.addActionListener(this);
        trainRadioButton.addActionListener(this);
		utilityRadioButton.addActionListener(this);
		
		radioPanel = new JPanel();
		//radioPanel.add(text1);
		radioPanel.add(lotRadioButton);
		radioPanel.add(lotComboBox);
		radioPanel.add(trainRadioButton);
		radioPanel.add(trainComboBox);
		radioPanel.add(utilityRadioButton);
		radioPanel.add(utilityComboBox);
			
		deedPanel = new JPanel();
		deedPanel.add(deedLabel);
		deedPanel.add(ownerLabel);
		
		buttonPanel = new JPanel();
		buttonPanel.add(viewButton);
		buttonPanel.add(closeButton);
		
		deedLabel.setBounds(10, 10, 300, 413);
		ownerLabel.setBounds(10, 428, 300, 20);
		lotRadioButton.setBounds(305, 10, 45, 18);
		lotComboBox.setBounds(305, 33, 169, 25);
		trainRadioButton.setBounds(305, 63, 45, 18);
		trainComboBox.setBounds(305, 86, 169, 25);
		utilityRadioButton.setBounds(305, 116, 45, 18);
		utilityComboBox.setBounds(305, 139, 169, 25);
		
		layeredPane.add(radioPanel, new Integer(0), 0);
		layeredPane.add(deedPanel, new Integer(0), 0);
		layeredPane.add(buttonPanel, new Integer(0), 0);
		
		frame.getContentPane().add(layeredPane);
		
		frame.pack(); 
		frame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
        
		if (lotRadioButton.isSelected() == true) {
		
			lotComboBox.setEnabled(true);
			trainComboBox.setEnabled(false);
			utilityComboBox.setEnabled(false);
		}
		
		if (trainRadioButton.isSelected() == true) {
		
			lotComboBox.setEnabled(false);
			trainComboBox.setEnabled(true);
			utilityComboBox.setEnabled(false);
		}
		
		if (utilityRadioButton.isSelected() == true) {
			
			lotComboBox.setEnabled(false);
			trainComboBox.setEnabled(false);
			utilityComboBox.setEnabled(true);
		}
    }
	
	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
		frame.setVisible(false);
    }
	
	private void viewButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
		
		JRadioButton selected = this.getSelected(group);
		String imageURL;
		
		if (selected == lotRadioButton) {
			
			imageURL = LotTiles.values()[lotComboBox.getSelectedIndex()].getTileURL();
		}
		
		else if (selected == trainRadioButton) {
			
			imageURL = TrainTiles.values()[trainComboBox.getSelectedIndex()].getTileURL();
		}
		
		else {
			
			imageURL = UtilitiesTiles.values()[utilityComboBox.getSelectedIndex()].getTileURL();
		}
		
		deedLabel.setIcon(createImageIcon(imageURL));
		deedLabel.repaint();
    }

	public JRadioButton getSelected(ButtonGroup group) {
		
		for (Enumeration E = group.getElements(); E.hasMoreElements();) {
			
			JRadioButton b = (JRadioButton)E.nextElement();
        
			if (b.getModel() == group.getSelection()) {
				
				return b;
			}
		}
		
		return null;
	}
	
	protected static ImageIcon createImageIcon(String path) {
		
        java.net.URL imgURL = BoardGUI.class.getResource(path);
		
        if (imgURL != null) {
		
            return new ImageIcon(imgURL);
        }
		
		else {
		
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
	
	public void populateLists() {
		
		for (int x = 0; x < 22; x++) {
			
			lotsList[x] = LotTiles.values()[x].getLotName();
		}
		
		for (int x = 0; x < 4; x++) {
			
			trainsList[x] = TrainTiles.values()[x].getTrainName();
		}
		
		for (int x = 0; x < 2; x++) {
			
			utilitiesList[x] = UtilitiesTiles.values()[x].getUtilityName();
		}
	}	
}